﻿/******************************************************   
创建者: ljj
创建时间: 2018-09-10
修改:
修改时间: 
功能: 微信生成二维码
 ******************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.kmer.ApiServer;
using ZXing;
using ZXing.QrCode;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using com.kmer;
using com.kmer.ApiServer.Attributes;
using com.kmer.Attributes;
using com.kmer.db;
using com.kmer.Helper;
using WxNewOrderAPI.model;

namespace WxNewOrderAPI
{
    /// <summary>
    /// 微信生成二维码
    /// </summary>
    public class WXNewCodeHandler : WXBaseHandler
    {
        /// writer:ljj
        /// Create Date：2018-09-10
        [Description("生成二维码")]
        [ReturnType(typeof(string), "文件路径")]
        public ApiResult CreateWXCode([ParamDescription("来源账号")] string SourceAccount,
            [ParamDescription("账号权限")] int AccountType = 1, [ParamDescription("是否企业微信")] bool Iswxwork = false)
        {
            try
            {
                var dc = new km_platformEntities();
                var user = dc.t_order_new_wx_account.FirstOrDefault(d => d.OpenId == SourceAccount &&
                                                                     (!Iswxwork || d.Iswxwork == Iswxwork) &&
                                                                     !d.IsDisable && !d.IsRemove && d.AccountType == 0);
                if (user == null) return Error("该用户没有权限操作!");
                var strPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //获取或设置包含该应用程序的目录的名称。
                if (!Directory.Exists(strPath + "WXCodePic"))
                {
                    Directory.CreateDirectory(strPath + "WXCodePic"); //不存在就创建文件夹 } 
                }
                var dyInfo = new DirectoryInfo(strPath + "WXCodePic");
                //获取文件夹下所有的文件
                foreach (FileInfo feInfo in dyInfo.GetFiles())
                {
                    //判断文件日期是否小于5分钟，是则删除
                    if (feInfo.CreationTime < DateTime.Now.AddMinutes(-5))
                        feInfo.Delete();
                }
                var writer = new BarcodeWriter
                {
                    Format = BarcodeFormat.QR_CODE
                };
                var options = new QrCodeEncodingOptions
                {
                    DisableECI = true,
                    //设置内容编码
                    CharacterSet = "UTF-8",
                    //设置二维码的宽度和高度
                    Width = 500,
                    Height = 500,
                    //设置二维码的边距,单位不是固定像素
                    Margin = 1,
                };
                writer.Options = options;

                var checkCode = Guid.NewGuid();

                var codeMessage = new WXNewModel.CodeModel
                {
                    SourceOpenId = SourceAccount,
                    AccountType = AccountType,
                    CheckCode = checkCode,
                };

                var codeJson = codeMessage.ToJson();

                var map = writer.Write(codeJson);

                var filePath = $"\\WXCodePic\\{checkCode:N}.png";
                map.Save(strPath + filePath, ImageFormat.Png);
                map.Dispose();
                return Success(filePath);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return Error(ex);
            }
        }
    }
}
